#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: demo.py 
@version:
@time: 2022/06/22 
"""

import socket
import time

MaxBytes = 1024 * 1024

server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server.settimeout(60)
host = '127.0.0.1'
port = 11224
address = (host, port)
server.bind(address)  # 绑定端口

try:
    while True:
        data, addr = server.recvfrom(MaxBytes)
        if not data:
            server.sendto('not get data'.encode(), addr)
        else:
            localTime = time.asctime(time.localtime(time.time()))
            print(localTime, )
            print('receive byte length:', len(data))
            print('receive byte data:', data.decode())
            server.sendto(f'get data: {data.decode()}'.encode(), addr)
except BaseException as e:
    print("出现异常：")
    print(repr(e))
finally:
    server.close()  # 关闭连接
